package org.jeecg.modules.yfXuessswh.service.impl;

import org.jeecg.modules.yfXuessswh.entity.HqYfXuessswh;
import org.jeecg.modules.yfXuessswh.entity.HqYfXuessswhYjxx;
import org.jeecg.modules.yfXuessswh.entity.HqYfXuessswhSssbxx;
import org.jeecg.modules.yfXuessswh.mapper.HqYfXuessswhYjxxMapper;
import org.jeecg.modules.yfXuessswh.mapper.HqYfXuessswhSssbxxMapper;
import org.jeecg.modules.yfXuessswh.mapper.HqYfXuessswhMapper;
import org.jeecg.modules.yfXuessswh.service.IHqYfXuessswhService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 营区学生宿舍维护
 * @Author: jeecg-boot
 * @Date:   2020-07-12
 * @Version: V1.0
 */
@Service
public class HqYfXuessswhServiceImpl extends ServiceImpl<HqYfXuessswhMapper, HqYfXuessswh> implements IHqYfXuessswhService {

	@Autowired
	private HqYfXuessswhMapper hqYfXuessswhMapper;
	@Autowired
	private HqYfXuessswhYjxxMapper hqYfXuessswhYjxxMapper;
	@Autowired
	private HqYfXuessswhSssbxxMapper hqYfXuessswhSssbxxMapper;
	
	@Override
	@Transactional
	public void saveMain(HqYfXuessswh hqYfXuessswh, List<HqYfXuessswhYjxx> hqYfXuessswhYjxxList,List<HqYfXuessswhSssbxx> hqYfXuessswhSssbxxList) {
		hqYfXuessswhMapper.insert(hqYfXuessswh);
		if(hqYfXuessswhYjxxList!=null && hqYfXuessswhYjxxList.size()>0) {
			for(HqYfXuessswhYjxx entity:hqYfXuessswhYjxxList) {
				//外键设置
				entity.setZhubId(hqYfXuessswh.getId());
				hqYfXuessswhYjxxMapper.insert(entity);
			}
		}
		if(hqYfXuessswhSssbxxList!=null && hqYfXuessswhSssbxxList.size()>0) {
			for(HqYfXuessswhSssbxx entity:hqYfXuessswhSssbxxList) {
				//外键设置
				entity.setZhubId(hqYfXuessswh.getId());
				hqYfXuessswhSssbxxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HqYfXuessswh hqYfXuessswh,List<HqYfXuessswhYjxx> hqYfXuessswhYjxxList,List<HqYfXuessswhSssbxx> hqYfXuessswhSssbxxList) {
		hqYfXuessswhMapper.updateById(hqYfXuessswh);
		
		//1.先删除子表数据
		hqYfXuessswhYjxxMapper.deleteByMainId(hqYfXuessswh.getId());
		hqYfXuessswhSssbxxMapper.deleteByMainId(hqYfXuessswh.getId());
		
		//2.子表数据重新插入
		if(hqYfXuessswhYjxxList!=null && hqYfXuessswhYjxxList.size()>0) {
			for(HqYfXuessswhYjxx entity:hqYfXuessswhYjxxList) {
				//外键设置
				entity.setZhubId(hqYfXuessswh.getId());
				hqYfXuessswhYjxxMapper.insert(entity);
			}
		}
		if(hqYfXuessswhSssbxxList!=null && hqYfXuessswhSssbxxList.size()>0) {
			for(HqYfXuessswhSssbxx entity:hqYfXuessswhSssbxxList) {
				//外键设置
				entity.setZhubId(hqYfXuessswh.getId());
				hqYfXuessswhSssbxxMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hqYfXuessswhYjxxMapper.deleteByMainId(id);
		hqYfXuessswhSssbxxMapper.deleteByMainId(id);
		hqYfXuessswhMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hqYfXuessswhYjxxMapper.deleteByMainId(id.toString());
			hqYfXuessswhSssbxxMapper.deleteByMainId(id.toString());
			hqYfXuessswhMapper.deleteById(id);
		}
	}
	
}
